package cartagows;

import java.util.Iterator;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axis2.context.MessageContext;



public class MsgInfo {
	
	private MessageContext ctx;
	private SOAPEnvelope envelope;
	private boolean served;	
	private String messageId;
	private String to;
	private String from;
	private String action;
	private OMElement replyTo;
	private OMElement faultTo;
	private String relatesTo;	
	private String opName;
	
	private WSMsgInfo wsMsgInfo;
	
	public MsgInfo(MessageContext ctx){
		this.ctx = ctx; 
		envelope = ctx.getEnvelope();
		served = false;

		SOAPHeader header = envelope.getHeader();
		Iterator<OMElement> it = header.getChildElements();
		while (it.hasNext()){
			OMElement el = it.next();
			//log.info("NODE: "+el.getLocalName());
			String tag = el.getLocalName();
			if (tag.equals("MessageID")){
				messageId = el.getText();
			} else if (tag.equals("To")){
				to = el.getText();
			} else if (tag.equals("From")){
				from = el.getText();
			} else if (tag.equals("ReplyTo")){
				replyTo = el;
				
			} else if (tag.equals("FaultTo")){
				faultTo = el;
			} else if (tag.equals("Action")){
				action = el.getText();
			} else if (tag.equals("RelatesTo")){
				relatesTo = el.getText();
			}
		}				 
		compileWSMsgInfo();
	}

	public String getWSATo(){
		return to;
	}

	public String getWSAFrom(){
		return from;
	}
	
	public String getWSAReplyToAddress(){
		//return replyTo.getFirstChildWithName(new javax.xml.namespace.QName("wsa","Address")).getText();
		return replyTo.getText();	
	}
	
	public String getWSAFaultTo(){
		return faultTo.getFirstChildWithName(new javax.xml.namespace.QName("wsa","Address")).getText();
	}
	
	public String getWSAAction(){
		return action;
	}
	
	public String getWSARelatesTo(){
		return relatesTo;
	}
	
	public String getWSAMessageId(){
		return messageId;
	}
	
	public void setOpName(String name){
		opName = name;
		wsMsgInfo.setOpName(name);
	}
	
	public String getOperationName(){
		return opName;
	}
	
	public OMElement getMsgContent(){
		return envelope.getBody().getFirstElement();
	}
	
	public void setServed(){
		served = true;
	}
	
	public boolean isServed(){
		return served;
	}
	
	public SOAPEnvelope getEnvelope(){
		return envelope;
	}

	public SOAPBody getBody(){
		return envelope.getBody();
	}

	public SOAPHeader getHeader(){
		return envelope.getHeader();
	}

	MessageContext getContext(){
		return ctx;
	}
	
	public WSMsgInfo getWSMsgInfo(){
		return wsMsgInfo;
	}
	
	private void compileWSMsgInfo(){
		WSMsgInfo msg = new WSMsgInfo();

		msg.opName = opName;
		msg.envelope = envelope.toString();
		msg.msgContent = envelope.getBody().getFirstElement().toString();

		SOAPHeader head = envelope.getHeader();
		msg.header = head.toString();
		msg.body = envelope.getBody().toString();
		Iterator<OMElement> it = head.getChildElements();
		while (it.hasNext()){
			OMElement el = it.next();
			String tag = el.getLocalName();
			if (tag.equals("MessageID")){
				msg.messageId = el.getText();
			} else if (tag.equals("To")){
				msg.to = el.getText();
			} else if (tag.equals("From")){
				msg.from = el.getText();
			} else if (tag.equals("ReplyTo")){
				msg.replyTo = el.toString();
				OMElement oel = el.getFirstElement();
				if (oel!=null){
					msg.replyToAddress = oel.getText();
				}
			} else if (tag.equals("FaultTo")){
				msg.faultTo = el.toString();
				OMElement oel = el.getFirstChildWithName(new javax.xml.namespace.QName("wsa","Address"));
				if (oel!=null){
					msg.faultToAddress = oel.getText();
				}
			} else if (tag.equals("Action")){
				msg.action = el.getText();
			} else if (tag.equals("RelatesTo")){
				msg.relatesTo = el.getText();
			}
		}		
		wsMsgInfo = msg;
	}
}
